package comp6231.project.common;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class ReplicaState implements Serializable {

	private Hashtable<String, Integer> items = new Hashtable<String, Integer>();
	private Hashtable<String, Hashtable<String, Integer>> customerRecords =
				new Hashtable<String, Hashtable<String, Integer>>();

	public void addItem(String itemID, int numberOfItem) {
		items.put(itemID, numberOfItem);
	}
	
	public void setCustomerRecords(Hashtable<String, Hashtable<String, Integer>> customerRecords){
		this.customerRecords = customerRecords;
	}
	
	public void addCustomerBoughtItem(String customerID, String itemID, int numberOfItem) {
		if (!customerRecords.containsKey(customerID)) {
			Hashtable<String, Integer> items = new Hashtable<String, Integer>();
			customerRecords.put(customerID, items);
		}
		customerRecords.get(customerID).put(itemID, numberOfItem);		
	}
	
	public Map<String, Integer> getItems() {
		return items;
	}
	
	public Set<String> getCustomers() {
		return customerRecords.keySet();
	}
	
	public Map<String, Integer> getCustomerBoughtItems(String customerID) {
		
		if (customerRecords.containsKey(customerID)) {
			return customerRecords.get(customerID);
		}
		return null;
	}
	
	public String toString() {
		return "ReplicaState[items: " + getItems() + ", customerRecords: " + getCustomers() + "]";
	}
	
	public static void main(String[] args) {
		ReplicaState s = new ReplicaState();
		s.addItem("0001", 1);
		s.addItem("0002", 1);
		s.addItem("0003", 1);
		s.addItem("0004", 1);
		s.addItem("0005", 1);
		s.addCustomerBoughtItem("000001", "0001", 1);
		s.addCustomerBoughtItem("000002", "0001", 1);
		s.addCustomerBoughtItem("000003", "0001", 1);
		s.addCustomerBoughtItem("000004", "0001", 1);
		s.addCustomerBoughtItem("000005", "0001", 1);
		s.addCustomerBoughtItem("000006", "0001", 1);
		byte[] buf = Utils.toBytes(s);
		ReplicaState s2 = (ReplicaState)Utils.fromBytes(buf);
		int a = 0;
	}	
}
